JasmineJS এ custom matchers তৈরি করা সম্ভব, যা আপনার টেস্ট কেসগুলোর জন্য নির্দিষ্ট এবং প্রয়োজনীয় যাচাইকরণ যুক্ত করতে সাহায্য করে। Custom matchers তৈরি করা আপনাকে আরো স্পেসিফিক এবং কাস্টম টেস্টিং লজিক প্রয়োগ করতে সহায়তা করে। এই ধরনের matchers বিশেষভাবে উপকারী যখন আপনি পুনরায় ব্যবহৃত যাচাইকরণ বা লজিক প্রয়োজন হয়। তবে, সঠিকভাবে custom matchers তৈরি এবং ব্যবহার করার জন্য কিছু best practices অনুসরণ করা গুরুত্বপূর্ণ।
1. Clear এবং Concise Matcher নাম নির্বাচন করুন
Custom matcher তৈরির সময়, তার নাম নির্বাচন করা খুবই গুরুত্বপূর্ণ। matcher নামটি অবশ্যই আপনার যাচাইকৃত লজিকের সাথে সম্পর্কিত এবং পরিষ্কার হওয়া উচিত। এটি অন্য ডেভেলপারদের বুঝতে সাহায্য করবে যে matcherটি কী কাজ করে।
উদাহরণ:
- সঠিক নাম:
toBeEvenNumber(),toBePositive() - ভুল নাম:
checkEven(),validatePositive()
// সঠিক উদাহরণ:
expect(4).toBeEvenNumber();
এখানে, toBeEvenNumber() নামটি স্পষ্টভাবে বুঝাচ্ছে যে এটি সংখ্যার even (যুগ্ম) হতে যাচাই করছে।
2. Matcher ফাংশনটি সিম্পল এবং স্পষ্ট রাখুন
Custom matcher এর ভিতরের লজিকটি যতটা সম্ভব সহজ এবং পরিষ্কার রাখুন। এটি আপনার কোডের রিডেবিলিটি বাড়াবে এবং পরবর্তীতে ডিবাগ করা সহজ করবে।
উদাহরণ:
beforeEach(function() {
jasmine.addMatchers({
toBeEvenNumber: function() {
return {
compare: function(actual) {
const result = {};
result.pass = actual % 2 === 0;
result.message = result.pass ?
`${actual} is an even number` :
`${actual} is not an even number`;
return result;
}
};
}
});
});
it("should check if the number is even", function() {
expect(4).toBeEvenNumber(); // সঠিক
expect(3).toBeEvenNumber(); // ভুল
});
এখানে, matcher খুব সহজ এবং পরিষ্কারভাবে একটি সংখ্যার even বা odd থাকা যাচাই করছে।
3. Custom Matcher কে Jasmine addMatchers() দিয়ে রেজিস্টার করুন
JasmineJS এ custom matcher রেজিস্টার করার জন্য jasmine.addMatchers() মেথডটি ব্যবহার করা হয়। এটি আপনার custom matchers কে Jasmine এর সাথে যুক্ত করে।
উদাহরণ:
beforeEach(function() {
jasmine.addMatchers({
toBePositive: function() {
return {
compare: function(actual) {
const result = {};
result.pass = actual > 0;
result.message = result.pass ?
`${actual} is a positive number` :
`${actual} is not a positive number`;
return result;
}
};
}
});
});
এখানে, toBePositive নামক custom matcher টি beforeEach() ব্লকের মধ্যে রেজিস্টার করা হয়েছে। এটি প্রতিটি টেস্টের আগে অ্যাক্সেসযোগ্য হবে।
4. বিভিন্ন ধরণের ইনপুট পরীক্ষা করুন
Custom matcher তৈরি করার সময়, আপনার matcher যেন বিভিন্ন ধরণের ইনপুটের জন্য সঠিকভাবে কাজ করে, সে ব্যাপারে নিশ্চিত হন। উদাহরণস্বরূপ, সংখ্যা, স্ট্রিং, বা অবজেক্টের ক্ষেত্রে বিভিন্ন ফলাফল যাচাই করা জরুরি।
উদাহরণ:
beforeEach(function() {
jasmine.addMatchers({
toBeNonEmptyString: function() {
return {
compare: function(actual) {
const result = {};
result.pass = typeof actual === 'string' && actual.trim() !== '';
result.message = result.pass ?
`"${actual}" is a non-empty string` :
`"${actual}" is not a non-empty string`;
return result;
}
};
}
});
});
it("should check if the string is non-empty", function() {
expect("Hello").toBeNonEmptyString(); // সঠিক
expect(" ").toBeNonEmptyString(); // ভুল
});
এখানে, matcherটি স্ট্রিং টাইপের ইনপুট নিয়ে যাচাই করছে এবং সঠিকভাবে empty স্ট্রিংও শনাক্ত করছে।
5. উপযুক্ত এবং স্পষ্ট মেসেজ প্রদান করুন
Custom matcher ব্যবহার করার সময় উপযুক্ত এবং স্পষ্ট মেসেজ প্রদান করা খুব গুরুত্বপূর্ণ, যাতে টেস্ট ফেইল হলে দ্রুত সমস্যার সমাধান করা যায়। Jasmine এর message প্রোপার্টি ব্যবহার করে আপনি এই মেসেজ প্রদান করতে পারেন।
উদাহরণ:
beforeEach(function() {
jasmine.addMatchers({
toBePositive: function() {
return {
compare: function(actual) {
const result = {};
result.pass = actual > 0;
result.message = result.pass ?
`${actual} is a positive number` :
`${actual} is not a positive number`;
return result;
}
};
}
});
});
it("should display the correct message", function() {
const result = expect(5).toBePositive();
expect(result.message).toBe("5 is a positive number");
const result2 = expect(-3).toBePositive();
expect(result2.message).toBe("-3 is not a positive number");
});
এখানে, যদি matcherটি ফেইল করে, তখন Jasmine নির্দিষ্ট করে জানাবে কেন টেস্টটি ব্যর্থ হয়েছে, যেমন "5 is not a positive number"।
6. Custom Matcher টেস্ট করুন
Custom matcher তৈরি করার পর, অবশ্যই এর কাজের সঠিকতা পরীক্ষা করুন। matcher টেস্ট করার সময় নিশ্চিত করুন যে এটি সঠিকভাবে কাজ করছে এবং প্রত্যাশিত ফলাফল প্রদান করছে।
উদাহরণ:
beforeEach(function() {
jasmine.addMatchers({
toBeEvenNumber: function() {
return {
compare: function(actual) {
const result = {};
result.pass = actual % 2 === 0;
result.message = result.pass ?
`${actual} is an even number` :
`${actual} is not an even number`;
return result;
}
};
}
});
});
it("should correctly check if a number is even", function() {
expect(4).toBeEvenNumber();
expect(3).not.toBeEvenNumber();
});
এখানে, আমরা সঠিকভাবে even number এবং odd number এর জন্য matcher যাচাই করেছি।
7. Reuseability এর কথা ভাবুন
Custom matchers তৈরি করার সময় তাদের reuseable (পুনঃব্যবহারযোগ্য) এবং modular (মডুলার) রাখতে চেষ্টা করুন। একে একাধিক টেস্টে ব্যবহার করার জন্য তৈরি করুন, যাতে আপনার টেস্ট কেসগুলো আরো সংক্ষিপ্ত এবং কার্যকর হয়।
উদাহরণ:
beforeEach(function() {
jasmine.addMatchers({
toBeWithinRange: function() {
return {
compare: function(actual, min, max) {
const result = {};
result.pass = actual >= min && actual <= max;
result.message = result.pass ?
`${actual} is within the range ${min} - ${max}` :
`${actual} is not within the range ${min} - ${max}`;
return result;
}
};
}
});
});
it("should check if number is within range", function() {
expect(10).toBeWithinRange(5, 15); // সঠিক
expect(20).not.toBeWithinRange(5, 15); // ভুল
});
এখানে, toBeWithinRange() matcher টির মাধ্যমে বিভিন্ন টেস্ট কেসে নির্দিষ্ট একটি রেঞ্জের মধ্যে মান থাকা যাচাই করা হচ্ছে।
সারাংশ
JasmineJS এ custom matchers ব্যবহার করার সময় কিছু গুরুত্বপূর্ণ best practices অনুসরণ করা উচিত:
- স্পষ্ট নাম নির্বাচন: Matcher নামটি স্পষ্ট এবং বোধগম্য হওয়া উচিত।
- সরল এবং পরিষ্কার লজিক: Matcher ফাংশনটি সহজ এবং পরিষ্কার হওয়া উচিত।
- মেসেজ প্রদান: যখন matcher ফেইল করে, তখন স্পষ্ট এবং বোধগম্য মেসেজ দেওয়া উচিত।
- রিইউজেবল কোড: Custom matcher গুলো যাতে বিভিন্ন টেস্টে ব্যবহার করা যায়, তা নিশ্চিত করুন।
- ইনপুট যাচাই: matcherটি যেন বিভিন্ন ধরণের ইনপুটের জন্য সঠিক কাজ করে তা নিশ্চিত করুন।
এই best practices গুলি অনুসরণ করলে আপনার custom matchers আরো কার্যকরী এবং রিডেবল হবে, যা আপনার Jasmine টেস্টিং প্রক্রিয়াকে আরও শক্তিশালী করবে।
Read more